<?php

namespace app\api\controller;
use app\common\model\Enum;
use app\api\model\HttpMachine;
use think\Db;
use think\facade\Cache;
use think\facade\Validate;

/**
 * 
 * Class Repeat
 * @package app\Repeat
 */
class Repeat extends Controller
{
    public function myOrderList()
    {
        
        $post = $this->request->post();
        $uid=$post["uid"];
        $time = date('Y-m-d H:i:s', strtotime("-5 minutes"));
        $map=[
            ['user_id', '=', $uid],
            ['pay_status', '=', Enum::PAY_SUCESS],
            ['create_at', '>', $time]
        ];
        $list=Db::table('order')->where($map)->order('create_at', 'desc')->select();
        foreach($list as &$value){ 
            $value['wash_type']=Enum::TransWashType($value['wash_type']);
        }
        return $this->renderSuccess(compact('list'));
    }


    public function chongxi()
    {
        $post = $this->request->post();
        $order_id=$post["order_id"];
        $map=[
            ['id', '=', $order_id]
        ];
        $order=Db::table('order')->where($map)->find();
        if(empty($order)){
            return $this->renderError('订单丢失或已支付!',true);
        }

        $time = date('Y-m-d H:i:s', strtotime("-5 minutes"));
        $create_at = $order["create_at"];

        if(strtotime($create_at)<strtotime($time)){
            return $this->renderError('订单已超时,不能再操作!',true);
        }

        $machineNo = $order["machine_no"];
        $action = $order["wash_type"];
        $data=array();

        $re = HttpMachine::GetMachineWorkStatus($machineNo);
        if($re != "1"){
            $data["re"]=3;
            $data["msg"]='洗衣机已在工作中,请查看!';
            return $this->renderSuccess(compact('data'));
        }

        $re=HttpMachine::SendWashCammand($machineNo,$action);
        if($re==1){
            $data["re"]=1;
            $data["msg"]='操作成功';
            return $this->renderSuccess(compact('data'));
        }else{
            $data["re"]=2;
            $data["msg"]='操作失败,请稍后重试';
            return $this->renderSuccess(compact('data'));
        }

    }
         

}

